//
//  SDGuideLoginViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/31.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDGuideLoginViewController.h"
#import "SDRegisterViewController.h"
#import "SDFogetPswdViewController.h"
#import "SDLoginManager.h"

@interface SDGuideLoginViewController()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)UIButton* skipBtn;
@property(nonatomic,strong)UIImageView* phoneNumberBgView;
@property(nonatomic,strong)UIImageView* passwordBgView;
@property(nonatomic,strong)UITextField* mobileNumberTextField;
@property(nonatomic,strong)UITextField* pswdTextField;

@end

#define kLeftMarginWidth 27

@implementation SDGuideLoginViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubviews];
    [self initData];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    self.navigationController.navigationBar.hidden = NO;
    [self dismissKeyboard];
}

-(void)initData
{

}

-(void)initSubviews
{
 
    self.automaticallyAdjustsScrollViewInsets = NO;
    BOOL naviHidden = [self navigationBarHidden];

    _tableView = [[UITableView alloc] initWithFrame:autoRectTableViewHaveBar(!naviHidden) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor colorWithRGB:0xf9fafc];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (BOOL)navigationBarHidden
{
    return YES;
}

-(void)dismissKeyboard
{
    [_mobileNumberTextField resignFirstResponder];
    [_pswdTextField resignFirstResponder];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
    if (row == 0) {
        return kScreenHeight;
    }
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    
    if (row == 0) {
        UITableViewCell* cell = nil;
        static NSString* identifier = @"skipCell";
        cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if( cell == nil )
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            UIImage* logo = [UIImage imageNamed:@"login_logo"];
            UIImageView* logoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNaviOrigenY + 52, logo.size.width, logo.size.height)];
            logoView.image = logo;
            logoView.left = (self.view.width - logoView.width)/2;
            [cell addSubview:logoView];
            
            UIButton* skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [skipBtn setTitle:@"跳过" forState:UIControlStateNormal];
            [skipBtn setTitleColor:[UIColor colorWithRGB:0x87909a] forState:UIControlStateNormal];
            skipBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            skipBtn.frame = CGRectMake(0, kNaviOrigenY + 22, 28, 14);
            skipBtn.right = kScreenWidth - 22;
            [skipBtn addTarget:self action:@selector(onSkipBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview: skipBtn];
            _skipBtn = skipBtn;
            _skipBtn.hidden = [self skipButtonHidden];

            //手机号
            _phoneNumberBgView = [[UIImageView alloc] initWithFrame:CGRectMake(kLeftMarginWidth, logoView.bottom + 50, self.view.width - 2*kLeftMarginWidth, 44)];
            CALayer * layer = [_phoneNumberBgView layer];
            layer.borderColor = [UIColor colorWithRGB:0xd0d7dd].CGColor;
            layer.borderWidth = 1.0f;
            _phoneNumberBgView.userInteractionEnabled = YES;
            _phoneNumberBgView.layer.cornerRadius = 22;
            _phoneNumberBgView.clipsToBounds = YES;
            [cell addSubview:_phoneNumberBgView];
            
            UIImage* mobileIcon = [UIImage imageNamed:@"login_icon_mobile.png"];
            UIImageView* mobileIconView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 0, mobileIcon.size.width, mobileIcon.size.height)];
            mobileIconView.top = (_phoneNumberBgView.height - mobileIconView.height)/2;
            mobileIconView.image = mobileIcon;
            [_phoneNumberBgView addSubview:mobileIconView];
            
            _mobileNumberTextField = [[UITextField alloc] initWithFrame:CGRectMake(mobileIconView.right + 20, 0, _phoneNumberBgView.width - mobileIconView.right - 40, 44)];
            _mobileNumberTextField.centerY = mobileIconView.centerY;
            _mobileNumberTextField.delegate = self;
            _mobileNumberTextField.placeholder = @"手机号";
            _mobileNumberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _mobileNumberTextField.keyboardType=UIKeyboardTypeNumberPad;
            _mobileNumberTextField.textColor = [UIColor colorWithRGB:0x747f8b];
            [_mobileNumberTextField setFont:[UIFont systemFontOfSize:13.f]];
            [_phoneNumberBgView addSubview:_mobileNumberTextField];
            
            //密码
            _passwordBgView = [[UIImageView alloc] initWithFrame:CGRectMake(kLeftMarginWidth, _phoneNumberBgView.bottom + 14, self.view.width - 2*kLeftMarginWidth, 44)];
            _passwordBgView.userInteractionEnabled = YES;
            layer = [_passwordBgView layer];
            layer.borderColor = [UIColor colorWithRGB:0xd0d7dd].CGColor;
            layer.borderWidth = 1.0f;
            _passwordBgView.layer.cornerRadius = 22;
            _passwordBgView.clipsToBounds = YES;
            [cell addSubview:_passwordBgView];
            
            UIImage* pswdIcon = [UIImage imageNamed:@"login_icon_password.png"];
            UIImageView* pswdIconView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 0, mobileIcon.size.width, mobileIcon.size.height)];
            pswdIconView.top = (_passwordBgView.height - pswdIconView.height)/2;
            pswdIconView.image = pswdIcon;
            [_passwordBgView addSubview:pswdIconView];
            
            _pswdTextField = [[UITextField alloc] initWithFrame:CGRectMake(pswdIconView.right + 20, 0, _passwordBgView.width - pswdIconView.right - 40, 44)];
            _pswdTextField.centerY = pswdIconView.centerY;
            _pswdTextField.delegate = self;
            _pswdTextField.placeholder = @"密码";
            _pswdTextField.secureTextEntry = YES;
            _pswdTextField.autocorrectionType = UITextAutocorrectionTypeNo;
            _pswdTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
            _pswdTextField.returnKeyType = UIReturnKeyDone;
            _pswdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _pswdTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            _pswdTextField.textColor = [UIColor colorWithRGB:0x747f8b];
            [_pswdTextField setFont:[UIFont systemFontOfSize:13.f]];
            [_passwordBgView addSubview:_pswdTextField];
            
            UIButton* forgetPswdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [forgetPswdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
            [forgetPswdBtn setTitleColor:[UIColor colorWithRGB:0x87909a] forState:UIControlStateNormal];
            forgetPswdBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            forgetPswdBtn.frame = CGRectMake(0, _passwordBgView.bottom + 14, 60, 12);
            forgetPswdBtn.right = skipBtn.right;
            [forgetPswdBtn addTarget:self action:@selector(onForgetPswdBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview: forgetPswdBtn];
            
            UIButton* loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [loginBtn setBackgroundColor:[UIColor colorWithRGB:0x24d9c6]];
            [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
            [loginBtn setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
            loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            loginBtn.frame = CGRectMake(kLeftMarginWidth, forgetPswdBtn.bottom + 28, self.view.width - 2*kLeftMarginWidth, 44);
            loginBtn.layer.cornerRadius = 22;
            loginBtn.clipsToBounds = YES;
            [loginBtn addTarget:self action:@selector(onLoginBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:loginBtn];
            
            UIButton* registerPswdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [registerPswdBtn setTitle:@"没有账号？注册" forState:UIControlStateNormal];
            [registerPswdBtn setTitleColor:[UIColor colorWithRGB:0x24d9c6] forState:UIControlStateNormal];
            registerPswdBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            registerPswdBtn.frame = CGRectMake(0, loginBtn.bottom + 24, 140, 16);
            registerPswdBtn.left = (self.view.width - registerPswdBtn.width)/2;
            [registerPswdBtn addTarget:self action:@selector(onRegisterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview: registerPswdBtn];

        }
        return cell;
    }
    return nil;
}

-(BOOL)skipButtonHidden
{
    return _skipBtnHidden;
}

#pragma mark - UIScrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self dismissKeyboard];
}

#pragma mark UI Event
-(void)onSkipBtnClicked:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kSipLoginNotificaion object:nil userInfo:nil];
}

-(void)onRegisterBtnClicked:(id)sender
{
    SDRegisterViewController* registerViewCtrl = [[SDRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerViewCtrl animated:YES];
}

-(void)onForgetPswdBtnClicked:(id)sender
{
    SDFogetPswdViewController* fogetPswdViewCtrl = [[SDFogetPswdViewController alloc] init];
    [self.navigationController pushViewController:fogetPswdViewCtrl animated:YES];
}

-(void)onLoginBtnClicked:(id)sender
{
    // 登录校验
    if (_mobileNumberTextField.text.length <= 0 || _pswdTextField.text.length <= 0) {
        
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"QQ帐号和密码不能为空" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
        return;
    }
    
    [[SDLoginManager sharedInstance] doLoginWithUserName:_mobileNumberTextField.text password:_pswdTextField.text];
}

-(void)tapGestureRecognizer:(UITapGestureRecognizer*)recognizer
{
    [self dismissKeyboard];
}

#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:_mobileNumberTextField]) {
        CALayer * layer = [_phoneNumberBgView layer];
        layer.borderColor = [UIColor colorWithRGB:0x24d9c6].CGColor;
        
        layer = [_passwordBgView layer];
        layer.borderColor = [UIColor colorWithRGB:0xd0d7dd].CGColor;
        //        _phoneNumberBgView.layer.shadowColor = [UIColor colorWithRGB:0x24d9c6].CGColor;
        //        _phoneNumberBgView.layer.shadowOffset = CGSizeMake(0, 0);
        //        _phoneNumberBgView.layer.shadowOpacity = 0.5;
        //        _phoneNumberBgView.layer.shadowRadius = 10.0;
        
    }else if ([textField isEqual:_pswdTextField]) {
        CALayer * layer = [_passwordBgView layer];
        layer.borderColor = [UIColor colorWithRGB:0x24d9c6].CGColor;
        
        layer = [_phoneNumberBgView layer];
        layer.borderColor = [UIColor colorWithRGB:0xd0d7dd].CGColor;
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
@end
